package org.leo.dm.example.flow;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import org.leo.dm.common.bean.ResultBean;
import org.leo.dm.common.bean.StepResultBean;
import org.leo.dm.component.engine.DefaultStorage;
import org.leo.dm.component.flow.Flow;
import org.leo.dm.component.util.DbBean;
import org.leo.dm.component.util.DbManager;
import org.leo.dm.component.util.DbTypeEnum;
import org.leo.dm.component.util.DmlEnum;
import org.leo.dm.example.base.FlowDaemon;
import org.leo.dm.example.base.GlobalVariables;
import org.leo.dm.example.callback.CallBackExample;
import org.leo.dm.example.callback.DbExceptionCallbackImpl;
import org.leo.dm.example.plugin.PluginColumnCustom;
import org.leo.dm.example.plugin.PluginQueueCustom;
import org.leo.dm.example.plugin.PluginRecordCustom;
import org.leo.dm.example.plugin.PluginSegmentCustom;
import org.leo.dm.example.reader.SaxXmlReader;
import org.leo.dm.example.writer.DbWriter;
import org.leo.dm.util.ThreadPoolUtil;
import org.leo.dm.util.ToUtil;
import org.leo.dm.util.comm.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FlowXmlToDbExample extends Flow {
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Override
	public ResultBean handle(Map<String, Object> param) {
		ResultBean resultBean = new ResultBean();
		try {
			// 初始化值域映射需要的集合
			GlobalVariables.rangeCodePlfcodeMap = new HashMap<String, Map<String, String>>();
			Map<String, String> codePlfcodeMap = new HashMap<String, String>();
			codePlfcodeMap.put("1", "F");
			codePlfcodeMap.put("2", "M");
			GlobalVariables.rangeCodePlfcodeMap.put("1|0.1", codePlfcodeMap);

			SaxXmlReader reader = new SaxXmlReader();
			reader.setColumnPlugin(new PluginColumnCustom());
			reader.setRecordPlugin(new PluginRecordCustom());
			reader.setSegmentPlugin(new PluginSegmentCustom());
			reader.setQueuePlugin(new PluginQueueCustom());

			reader.setData(new String(FileUtil.readFile("d:/shk_1116_1655z.xml")));// E:/xiao_h/test_new_64.xml
			reader.addExtendDataNames(new String[] { "GROUPID" });

			Connection conn = null;
			DbBean dbBean = null;

			dbBean = new DbBean();
			dbBean.setIp("192.168.147.127");
			dbBean.setPort("3306");
			dbBean.setInstance("di_ehr_310000001");
			dbBean.setUser("root");
			dbBean.setPassword("123456");
			dbBean.setType(DbTypeEnum.MYSQL);
			conn = DbManager.getConection(dbBean);

			DbWriter writer = new DbWriter();
			writer.setConn(conn);
			Map<String, String> tableNameMapping = new HashMap<String, String>();
			tableNameMapping.put("test3", "test4");
			tableNameMapping.put("test", "test2");
			writer.setTableNameMapping(tableNameMapping);
			writer.setCallback(new CallBackExample());
			writer.setDbExceptionCallback(new DbExceptionCallbackImpl());
			writer.setDmlEnum(DmlEnum.INSERT);
			DefaultStorage storage = new DefaultStorage(20000);
			storage.setReader(reader);
			storage.setWriter(writer);

			this.setReader(reader);
			this.setWriter(writer);
			this.setStorage(storage);

			this.run();

			ToUtil.to("其他操作");

			// 监控数据读写效率
			new FlowDaemon().handle(new ThreadPoolUtil(), reader, writer);
			// 同步等待返回值
			StepResultBean readerResultBean = reader.getResultBean();
			log.info("readerResultBean" + readerResultBean.toStr());
			StepResultBean writerResultBean = writer.getResultBean();
			log.info("writerResultBean" + writerResultBean.toStr());
			if ("-1".equals(readerResultBean.getCode()) || "-1".equals(writerResultBean.getCode())) {
				resultBean.setCode("-1");
			} else {
				resultBean.setCode("1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultBean.setCode("-1");
		}
		return resultBean;
	}

	public static void main(String[] args) {
		Map<String, Object> param = new HashMap<String, Object>();
		FlowXmlToDbExample flow = new FlowXmlToDbExample();
		flow.handle(param);
	}
}
